package com.gzc.just.play.last.war.sceneserver.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * 玩家背包实体类
 * 
 * 对应数据库表：player_inventory
 * 作为Phase 4.3数据库持久化集成的一部分
 */
@Entity
@Table(name = "player_inventory", 
       indexes = {
           @Index(name = "idx_inventory_player_id", columnList = "player_id"),
           @Index(name = "idx_inventory_item_id", columnList = "item_id"),
           @Index(name = "idx_inventory_item_type", columnList = "item_type")
       })
@EntityListeners(AuditingEntityListener.class)
public class PlayerInventory {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "player_id", nullable = false, foreignKey = @ForeignKey(name = "fk_inventory_player"))
    @JsonIgnore
    private Player player;
    
    @Column(name = "item_id", nullable = false)
    private Long itemId;
    
    @Column(name = "item_name", nullable = false, length = 100)
    private String itemName;
    
    @Column(name = "item_type", nullable = false)
    private Integer itemType;
    
    @Column(name = "quantity", nullable = false)
    private Integer quantity = 1;
    
    @Column(name = "quality", nullable = false)
    private Integer quality = 1;
    
    @Column(name = "level", nullable = false)
    private Integer level = 1;
    
    @Column(name = "is_equipped", nullable = false)
    private Boolean isEquipped = false;
    
    @Column(name = "slot_position")
    private Integer slotPosition;
    
    @Column(name = "attributes", columnDefinition = "TEXT")
    private String attributes;
    
    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    @LastModifiedDate
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;
    
    // 物品类型常量
    public static final int ITEM_TYPE_WEAPON = 1;      // 武器
    public static final int ITEM_TYPE_ARMOR = 2;       // 防具
    public static final int ITEM_TYPE_CONSUMABLE = 3;  // 消耗品
    public static final int ITEM_TYPE_MATERIAL = 4;    // 材料
    public static final int ITEM_TYPE_QUEST = 5;       // 任务物品
    public static final int ITEM_TYPE_CURRENCY = 6;     // 货币
    public static final int ITEM_TYPE_DECORATION = 7;  // 装饰品
    
    // 品质常量
    public static final int QUALITY_COMMON = 1;    // 普通
    public static final int QUALITY_RARE = 2;      // 精良
    public static final int QUALITY_EPIC = 3;      // 史诗
    public static final int QUALITY_LEGENDARY = 4; // 传说
    public static final int QUALITY_MYTHIC = 5;    // 神话
    
    // 构造函数
    public PlayerInventory() {}
    
    public PlayerInventory(Player player, Long itemId, String itemName, Integer itemType) {
        this.player = player;
        this.itemId = itemId;
        this.itemName = itemName;
        this.itemType = itemType;
    }
    
    // Getter和Setter方法
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public Player getPlayer() {
        return player;
    }
    
    public void setPlayer(Player player) {
        this.player = player;
    }
    
    public Long getItemId() {
        return itemId;
    }
    
    public void setItemId(Long itemId) {
        this.itemId = itemId;
    }
    
    public String getItemName() {
        return itemName;
    }
    
    public void setItemName(String itemName) {
        this.itemName = itemName;
    }
    
    public Integer getItemType() {
        return itemType;
    }
    
    public void setItemType(Integer itemType) {
        this.itemType = itemType;
    }
    
    public Integer getQuantity() {
        return quantity;
    }
    
    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }
    
    public Integer getQuality() {
        return quality;
    }
    
    public void setQuality(Integer quality) {
        this.quality = quality;
    }
    
    public Integer getLevel() {
        return level;
    }
    
    public void setLevel(Integer level) {
        this.level = level;
    }
    
    public Boolean getIsEquipped() {
        return isEquipped;
    }
    
    public void setIsEquipped(Boolean isEquipped) {
        this.isEquipped = isEquipped;
    }
    
    public Integer getSlotPosition() {
        return slotPosition;
    }
    
    public void setSlotPosition(Integer slotPosition) {
        this.slotPosition = slotPosition;
    }
    
    public String getAttributes() {
        return attributes;
    }
    
    public void setAttributes(String attributes) {
        this.attributes = attributes;
    }
    
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }
    
    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    // 业务方法
    public void addQuantity(int amount) {
        this.quantity += amount;
    }
    
    public boolean removeQuantity(int amount) {
        if (this.quantity >= amount) {
            this.quantity -= amount;
            return true;
        }
        return false;
    }
    
    public void equip(Integer slot) {
        this.isEquipped = true;
        this.slotPosition = slot;
    }
    
    public void unequip() {
        this.isEquipped = false;
        this.slotPosition = null;
    }
    
    public String getItemTypeName() {
        switch (itemType) {
            case ITEM_TYPE_WEAPON:
                return "武器";
            case ITEM_TYPE_ARMOR:
                return "防具";
            case ITEM_TYPE_CONSUMABLE:
                return "消耗品";
            case ITEM_TYPE_MATERIAL:
                return "材料";
            case ITEM_TYPE_QUEST:
                return "任务物品";
            case ITEM_TYPE_CURRENCY:
                return "货币";
            case ITEM_TYPE_DECORATION:
                return "装饰品";
            default:
                return "未知类型";
        }
    }
    
    public String getQualityName() {
        switch (quality) {
            case QUALITY_COMMON:
                return "普通";
            case QUALITY_RARE:
                return "精良";
            case QUALITY_EPIC:
                return "史诗";
            case QUALITY_LEGENDARY:
                return "传说";
            case QUALITY_MYTHIC:
                return "神话";
            default:
                return "未知品质";
        }
    }
    
    @Override
    public String toString() {
        return "PlayerInventory{" +
                "id=" + id +
                ", player=" + (player != null ? player.getPlayerId() : null) +
                ", itemId=" + itemId +
                ", itemName='" + itemName + '\'' +
                ", itemType=" + itemType +
                ", itemTypeName='" + getItemTypeName() + '\'' +
                ", quantity=" + quantity +
                ", quality=" + quality +
                ", qualityName='" + getQualityName() + '\'' +
                ", level=" + level +
                ", isEquipped=" + isEquipped +
                ", slotPosition=" + slotPosition +
                ", createdAt=" + createdAt +
                '}';
    }
}